package cz.idsv.web.mvc.controller;

/**
 * Address Controller.
 * 
 * @author Jan Borzensky
 */
import java.util.Collection;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import cz.idsv.web.mvc.model.entity.AddressEntity;
import cz.idsv.web.mvc.model.service.AddressService;



/**
 * Address controller.
 * 
 * @author Jan Borzensky
 */
@Controller
@SessionAttributes(value = {"addressEntity"})
public class AddressController {
	
	private static final String LIST_MODEL_KEY = "list";
	private static final String ADDRESS_ENTITY = "addressEntity";
	
	protected static Logger logger = Logger.getLogger("addressController"); 
	
	@Autowired()
	private AddressService addressService;
	
	/**
     * <p>Searches for all product and returns them in a 
     * <code>Collection</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/address/list'.</p>
     */
    @RequestMapping(value="/address/list", method=RequestMethod.GET)
    public @ModelAttribute(LIST_MODEL_KEY) Collection<AddressEntity> search() {
    	logger.debug("Received request to show all address"); 
    	
        return addressService.findAddress();
    }
    
	/**
     * <p>Create new or update old address 
     * <code>AddressEntity</code>.</p>
     * 
     * <p>Expected HTTP GET and request '/address/form'.</p>
     */
    @RequestMapping(value="/address/form", method=RequestMethod.GET)
    public @ModelAttribute(ADDRESS_ENTITY)  AddressEntity getDocumentForm(@RequestParam(value = "id", required = false) Integer id) {
        if(id==null)return new AddressEntity();
        return addressService.findAddressById(id);
    }
    
	/**
     * <p>Save new or update old address 
     * <code>AddressEntity</code>.</p>
     * 
     * <p>Expected HTTP POST and response '/address/saveForm'.</p>
     */
    @RequestMapping(value="/address/saveForm", method=RequestMethod.POST)
    public String saveAdddressForm(AddressEntity addressEntity, Model model) {
    	if(addressEntity != null) 
    		addressService.save(addressEntity);
    	return "redirect:/address/list.html";
    }
    
    /**
     * <p>Deletes a address.</p>
     * 
     * <p>Expected HTTP POST and request '/address/delete'.</p>
     */
    @RequestMapping(value="/address/delete", method=RequestMethod.POST)
    public String delete(AddressEntity addressEntity) {
    	logger.debug("Received request to delete address with id:"+ addressEntity.getId());
        addressService.delete(addressEntity);
        return "redirect:/address/list.html";
    }
    
}
